<?php
/**
* @package      auto_occasion
* @subpackage   annonce
* @author       CMS
* @copyright    2011
* @license      All rights reserved
*/


/**
 * Classe permettant de gérer les témoignages 
 * ajout , rechercher
 *
 */
class BoTemoignageCtrl extends jController {
    
public $pluginParams = array(
                                    '*'=>array('auth.required'=>true,
                                    'jacl2.right'=>'admin.compte.gerer'
                                    )
                            );

    /**
     * Liste des témoignages
     *
     *
     */
    function index() {

        jClasses::inc("temoignage~temoignageSrv");
        jClasses::inc("temoignage~temoignageVendeurSrv");
        jClasses::inc("annonce~annonceCoordonneesSrv");

        $id                  = $this->intParam('id', '');
	
        $vendeur             = temoignageVendeurSrv::getAll();
        $departement         = annonceCoordonneesSrv::listeDepartement();

        $offset        =  $this->intParam('offset',0,true);
        $nbitemparpage = TEMOIGNAGE_NBITEMPARPAGE;
        $properties = array('start-label' => '',
                              'prev-label'  => '<',
                              'next-label'  => '>',
                              'end-label'   => '',
                              'area-size'   => 4);

        $liste       = temoignageSrv::getAll($offset, $nbitemparpage);
        $annonce     = ($id)? temoignageSrv::getIdAnnonce($id) : array();
        $annonce     = (sizeof($annonce) && isset($annonce[0]))? $annonce[0] : array();
        $temoignage  = ($id) ? temoignageSrv::chargeParId($id) : temoignageSrv::init();

		// on charge le vendeur par $temoignage->vendeur_id
        $vendeurbyid = temoignageVendeurSrv::getById ($temoignage->vendeur_id);


        $rep = $this->getResponse('html');
  		$rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.validation.js');

		$rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/css/jquery-ui.css');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery-ui.min.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'ckeditor/ckeditor.js');

		$rep->menusActifs = adminHtmlResponse::MENU_TEMOIGNAGE ;

        $rep->body->assign('liste',$liste['temoignage']);
        $rep->body->assign('recordCount',$liste['nbRecords']);
        $rep->body->assign('nbitemparpage', $nbitemparpage);
        $rep->body->assign('page', $offset);
        $rep->body->assign('properties', $properties);
		$rep->body->assign('vendeur', $vendeur);
        $rep->body->assign('departement', $departement);
		$rep->body->assign('id',      $id);
        $rep->body->assign('temoignage', $temoignage );
		$rep->body->assign('vendeurbyid', $vendeurbyid);
		$rep->body->assign('annonce', $annonce);


        $rep->bodyTpl = 'temoignage~BoTemoignageEdit';
        return $rep;
    }


   /**
     * sauvegarder un témoignage
     *
     *
     */
     function sauvegarder() {

        jClasses::inc("temoignage~temoignageSrv");

        $temoignage                            = new stdClass();
        $temoignage->temoignage_vendeur        = $this->stringParam('vendeur','');
        $temoignage->temoignage_titre          = $this->stringParam('titre','');
        $temoignage->temoignage_content        = $this->stringParam('content','');
        $temoignage->temoignage_departement    = $this->stringParam('departement','');
        $temoignage->temoignage_datevalide     = $this->stringParam('datevalide','');
        $temoignage->temoignage_daterefus      = $this->stringParam('daterefus','');
        $temoignage->temoignage_datesuppr      = $this->stringParam('datesuppr','');
        $temoignage->temoignage_status         = $this->intParam('status','');
        $temoignage->temoignage_id             = $this->intParam('id','');

        if ($temoignage->temoignage_id){
             $temoignage = temoignageSrv::modifier($temoignage);
        } else {
             $temoignage = temoignageSrv::ajouter($temoignage);
        }



        $rep = $this->getResponse('redirect');
        $rep->action = 'temoignage~BoTemoignage:index';
        return $rep;
    }

    /**
     * Supprimer un temoignage
     *
     */
    function supprimer() {
        $rep = $this->getResponse('redirect');

        jClasses::inc("commun~common");
    
        common::supprimer($this->intParam('id'), 'ao_temoignage');

        $rep->action = 'temoignage~BoTemoignage:index';
        return $rep;
    }


   }

